package com.xjrsoft.module.app.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.annotation.XjrLog;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.TreeUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.app.dto.*;
import com.xjrsoft.module.app.entity.*;
import com.xjrsoft.module.app.service.IAppMenuButtonService;
import com.xjrsoft.module.app.service.IAppMenuColumnService;
import com.xjrsoft.module.app.service.IAppMenuFormService;
import com.xjrsoft.module.app.service.IAppMenuService;
import com.xjrsoft.module.app.vo.AppMenuListVo;
import com.xjrsoft.module.app.vo.AppMenuPageVo;
import com.xjrsoft.module.app.vo.AppMenuSimpleTreeVo;
import com.xjrsoft.module.app.vo.AppMenuVo;
import com.xjrsoft.module.organization.entity.Role;
import com.xjrsoft.module.organization.vo.RoleListVo;
import com.xjrsoft.module.organization.vo.RolePageVo;
import com.xjrsoft.module.system.dto.MenuTreeDto;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.system.entity.MenuButton;
import com.xjrsoft.module.system.entity.MenuColumn;
import com.xjrsoft.module.system.service.IDictionarydetailService;
import com.xjrsoft.module.system.service.IDictionaryitemService;
import com.xjrsoft.module.system.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  app 菜单前端控制器
 * </p>
 *
 * @author tzx
 * @since 2023-07-26
 */
@RestController
@RequestMapping(GlobalConstant.APP_MODEL +"/menu")
@Api(value = GlobalConstant.APP_MODEL +"/menu", tags = "app 菜单模块")
@AllArgsConstructor
public class AppMenuController {

    private final IAppMenuService appMenuService;

    private final IAppMenuButtonService appMenuButtonService;

    private final IAppMenuColumnService appMenuColumnService;

    private final IAppMenuFormService appMenuFormService;

    private final IDictionarydetailService dictionarydetailService;

    @GetMapping("/page")
    @ApiOperation("app菜单分页接口")
    public R page(AppMenuPageDto dto){

        LambdaQueryWrapper<AppMenu> queryWrapper = Wrappers.lambdaQuery(AppMenu.class)
                .eq(StrUtil.isNotBlank(dto.getName()), AppMenu::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getCode()), AppMenu::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getCategoryId()), AppMenu::getCategoryId, dto.getCategoryId())
                .orderByAsc(AppMenu::getSortCode)
                .select(AppMenu.class, x -> VoToColumnUtil.fieldsToColumns(AppMenuPageVo.class).contains(x.getProperty()));

        IPage<AppMenu> page = appMenuService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<AppMenuPageVo> pageOutput = ConventPage.getPageOutput(page, AppMenuPageVo.class);
        return R.ok(pageOutput);
    }


    @GetMapping("/list")
    @ApiOperation("app菜单 列表 不分页")
    public R list(AppMenuListDto dto){

        LambdaQueryWrapper<AppMenu> queryWrapper = Wrappers.lambdaQuery(AppMenu.class)
                .eq(StrUtil.isNotBlank(dto.getName()), AppMenu::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getCode()), AppMenu::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getCategoryId()), AppMenu::getCategoryId, dto.getCategoryId())
                .eq(AppMenu::getEnabledMark, EnabledMark.ENABLED.getCode())
                .orderByAsc(AppMenu::getSortCode)
                .select(AppMenu.class, x -> VoToColumnUtil.fieldsToColumns(AppMenuPageVo.class).contains(x.getProperty()));


        List<AppMenu> list = appMenuService.list(queryWrapper);

        List<AppMenuListVo> appMenuListVos = BeanUtil.copyToList(list, AppMenuListVo.class);

        return R.ok(appMenuListVos);
    }

    @PostMapping
    @ApiOperation("新增")
    public R add(@Valid @RequestBody AddAppMenuDto dto){

        AppMenu appMenu = BeanUtil.toBean(dto, AppMenu.class);

        return R.ok(appMenuService.save(appMenu));

    }


    @PutMapping
    @ApiOperation("修改")
    public R update(@Valid @RequestBody UpdateAppMenuDto dto){

        AppMenu appMenu = BeanUtil.toBean(dto, AppMenu.class);

        return R.ok(appMenuService.updateById(appMenu));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @XjrLog(value = "删除app端展示页面设计")
    public R delete(@Valid @RequestBody List<Long> ids) {
        appMenuService.removeBatchByIds(ids);
        return R.ok(Boolean.TRUE);
    }

    @GetMapping("/simple-tree")
    @ApiOperation(value = "获取所有app菜单（树结构）-以分类为父级")
    public R menuAuthTree(@Valid MenuTreeDto dto) {
        List<AppMenu> list = appMenuService.getAppAuthMenuList(dto);
        List<DictionaryDetail> dictionaryDetailList = dictionarydetailService.list(Wrappers.<DictionaryDetail>lambdaQuery().eq(DictionaryDetail::getItemId, 1673142942973763585L));
        List<AppMenuSimpleTreeVo> appMenuSimpleTreeVoList = new ArrayList<>();
        if (dictionaryDetailList.size() > 0){
            for (DictionaryDetail dictionaryDetail : dictionaryDetailList) {//以功能分类做为父级
                AppMenuSimpleTreeVo appMenuSimpleTreeVo = new AppMenuSimpleTreeVo();
                appMenuSimpleTreeVo.setId(dictionaryDetail.getId());
                appMenuSimpleTreeVo.setCategoryId(dictionaryDetail.getId());
                appMenuSimpleTreeVo.setName(dictionaryDetail.getName());
                appMenuSimpleTreeVo.setCode(dictionaryDetail.getCode());
                List<AppMenu> collect = list.stream().filter(x -> x.getCategoryId().equals(dictionaryDetail.getId())).collect(Collectors.toList());
                List<AppMenuVo> appMenuVos = BeanUtil.copyToList(collect, AppMenuVo.class);
                appMenuSimpleTreeVo.setChildren(appMenuVos);
                appMenuSimpleTreeVoList.add(appMenuSimpleTreeVo);
            }
        }
        return R.ok(appMenuSimpleTreeVoList);
    }

    @GetMapping("/app-simple-tree")
    @ApiOperation(value = "获取所有app菜单（树结构）-以分类为父级")
    public R appMenuAuthTree(@Valid MenuTreeDto dto) {
        List<AppMenu> list = appMenuService.getAppAuthMenuList(dto);
        List<DictionaryDetail> dictionaryDetailList = dictionarydetailService.list(Wrappers.<DictionaryDetail>lambdaQuery().eq(DictionaryDetail::getItemId, 1673142942973763585L));
        List<AppMenuSimpleTreeVo> appMenuSimpleTreeVoList = new ArrayList<>();
        if (dictionaryDetailList.size() > 0){
            for (DictionaryDetail dictionaryDetail : dictionaryDetailList) {//以功能分类做为父级
                AppMenuSimpleTreeVo appMenuSimpleTreeVo = new AppMenuSimpleTreeVo();
                appMenuSimpleTreeVo.setId(dictionaryDetail.getId());
                appMenuSimpleTreeVo.setCategoryId(dictionaryDetail.getId());
                appMenuSimpleTreeVo.setName(dictionaryDetail.getName());
                appMenuSimpleTreeVo.setCode(dictionaryDetail.getCode());
                List<AppMenu> collect = list.stream().filter(x -> x.getCategoryId().equals(dictionaryDetail.getId())).collect(Collectors.toList());
                List<AppMenuVo> appMenuVos = BeanUtil.copyToList(collect, AppMenuVo.class);
                if (appMenuVos.size() > 0){
                    appMenuSimpleTreeVo.setChildren(appMenuVos);
                    appMenuSimpleTreeVoList.add(appMenuSimpleTreeVo);
                }
            }
        }
        return R.ok(appMenuSimpleTreeVoList);
    }



    @GetMapping("/button-list")
    @ApiOperation("app菜单按钮列表权限 不分页")
    public R buttonList(@RequestParam(required = false) Long menuId){
        if (menuId == null) {
            return R.ok(new ArrayList<>(0));
        }
        List<AppMenuButton> list = appMenuButtonService.list(Wrappers.<AppMenuButton>lambdaQuery().eq(menuId != 0, AppMenuButton::getMenuId, menuId));
        List<MenuButtonListVo> menuButtonListVos = BeanUtil.copyToList(list, MenuButtonListVo.class);
        return R.ok(menuButtonListVos);
    }

    @GetMapping("/column-list")
    @ApiOperation("app菜单列表字段权限 不分页")
    public R columnList(@RequestParam(required = false) Long menuId){
        if (menuId == null) {
            return R.ok(new ArrayList<>(0));
        }
        List<AppMenuColumn> list = appMenuColumnService.list(Wrappers.<AppMenuColumn>lambdaQuery().eq(menuId != 0, AppMenuColumn::getMenuId, menuId));
        List<MenuColumnListVo> menuColumnListVos = BeanUtil.copyToList(list, MenuColumnListVo.class);
        return R.ok(menuColumnListVos);
    }

    @GetMapping("/form-list")
    @ApiOperation("app菜单表单列表权限 不分页")
    public R formList(@RequestParam(required = false) Long menuId){
        if (menuId == null) {
            return R.ok(new ArrayList<>(0));
        }
        List<AppMenuForm> list = appMenuFormService.list(Wrappers.<AppMenuForm>lambdaQuery().eq(menuId != 0, AppMenuForm::getMenuId, menuId));
        List<MenuFormListVo> menuFormListVos = BeanUtil.copyToList(list, MenuFormListVo.class);
        return R.ok(TreeUtil.build(menuFormListVos));
    }

}
